//表格行上色
$("#my_tabcol tr").mouseover(function () {
    $(this).toggleClass("bcolor");
}).mouseout(function () {
    $(this).toggleClass("bcolor");
});

$(function () {

    let songid = getQueryString('songid');
    playmp3Init(songid);
    /**
     * 加载音乐
     */
    addsong(1);

    /**
     * 加载图片
     */
    addmainpic(1);
});

/**
 * 加载音乐
 */
function addsong(pageNum) {
    $.get(path + "/song/song/list-param", {"pageNum": pageNum, "pageSize": 10, "type": 1}, function (result) {
        if (result.code === 200) {
            $("#my_tabcol").empty();
            $("#pageChange").empty();
            $("#pageChange").append('<span class="pageSpan">页数:</span>');
			let list = result.list;
			let page = result.page;
			let index = 1;
			let musicList = new Array(page.pageSize);
			$(list).each(function () {
				let tr = '<tr><td class="firstTd">' + index + '</td>'
					+ '<td><span>' + this.songName + '</span><a href="/sites/player.html?songid=' + this.id + '"><img src="/image/img/mv.jpg" /></a></td>'
					+ '<td><a href="javascript:playmp3(' + this.mp3Id + ')"><img src="/image/img/erji2.jpg" title="播放" /></a>'
					+ '<a href="' + this.mp3 + '"><img src="/image/img/xiazai.jpg" title="下载" /></a>'
					+ '<a href="javascript:collectsong(' + this.id + ')"><img src="/image/img/collect.jpg" title="取消收藏" /></a></td></tr>';
				$("#my_tabcol").append(tr);
                $("#my_tabcol tr:last").data("song", this);
                index++;
                if (this.mp3 != null) {
                    musicList.push(this.mp3);
                }
            });

            for (var i = 1; i <= page.pageCount; i++) {
                var a = '<span class="pageSpan"><a href="javascript:addsong(' + i + ')">' + i + '</a></span>&nbsp;';
                $("#pageChange").append(a);
            }

            //存放音乐列表，根据音乐文件路径，进行填写
            playMusic(musicList);
        } else {
            alert(result.msg);
        }
    });
}

function playMusic(musicList) {
	let myAudio = new Audio();
	//是否进行预加载
    myAudio.preload = false;
    //是否显示隐藏按钮
    myAudio.controls = true;
    myAudio.id = "audio_mp3";
    //myAudio.hidden = true;
    //从音乐列表中，获取最后一个音乐（并删除）
	let src = musicList.pop();
	if (src == null) {
        src = musicList.pop();
    }
    myAudio.src = src;
    //将最后一个音乐添加到数组的开头，这样实现循环
    musicList.unshift(src);
    //绑定音乐结束事件，当音乐播放完成后，将会触发playEndedHandler方法
    myAudio.addEventListener("ended", playEndedHandler, false);
    $("#audio").empty();
    //播放当前音乐
    myAudio.play();
    document.getElementById("audio").appendChild(myAudio);
    //将循环播放关闭，如果开启，将不能触发playEndedHandler方法，只能进行单曲循环
    myAudio.loop = false;

    function playEndedHandler() {
        src = musicList.pop();
        myAudio.src = src;
        musicList.unshift(src);
        myAudio.play();
    }
}

function playmp3Init(songid) {
    $.get(path + "/song/song/get-param", {"id": songid, "type": 1}, function (result) {
        if (result.code === 200) {
            let data = result.data;
            $("#audio_mp3").attr("src", data.mp3);
        } else {
            alert(result.msg);
        }
    });
}

function playmp3(filesId) {
    $.get(path + "/system/files/get-one", {"id": filesId}, function (result) {
        if (result.code === 200) {
            let data = result.data;
            $("#audio_mp3").attr("src", data.url);
        } else {
            alert(result.msg);
        }
    });
}

function play_next() {
    $.get(path + "/system/files/get-one", {"id": filesId}, function (result) {
        if (result.code === 200) {
            let data = result.data;
            $("#audio_mp3").attr("src", data.url);
        } else {
            alert(result.msg);
        }
    });
}

function collectsong(songid) {
    $.post(path + "/song/song-collection/getParamById", {"id": songid}, function (result) {
        if (result.code == 200) {
            var data = result.data;
            //todo
        } else {
            alert(result.msg);
        }
    });
}

/**
 * 加载主页图片
 */
function addmainpic(pageNum) {
    $.get(path + "/system/picture/page-list", {"pageNum": pageNum, "pageSize": 15}, function (result) {
        if (result.code === 200) {
            $("#my_right").empty();
            let list = result.list;
            $(list).each(function () {
                let div = '<div id="my_pics"><img src="../image/img/' + this.picName + '"/></div>';
                $("#my_right").append(div);
            });
        } else {
            alert(result.msg);
        }
    });
}

